<?php
class WPaymentAuthorizenetIpn extends UWidgetWorklet
{
	public $layout = false;
	
	public function accessRules()
	{
		return array(array('allow','users'=>array('*')));
	}
	
	public function taskConfig()
	{
		if(app()->request->isSecureConnection)
			wm()->get('base.init')->requireSecure = true;
		wm()->get('base.init')->setState('subscribe',false);
		
		include_once(dirname(__FILE__).'/../components/anet_php_sdk/AuthorizeNet.php');
		
		defined("AUTHORIZENET_API_LOGIN_ID")
			|| define("AUTHORIZENET_API_LOGIN_ID", $this->param('apiLogin'));
	    defined("AUTHORIZENET_MD5_SETTING")
			|| define("AUTHORIZENET_MD5_SETTING", $this->param('apiMD5Hash'));
	    $message = new AuthorizeNetSIM;
	    if ($message->isAuthorizeNet() && $message->approved) {
	        $transactionId = $message->transaction_id;
	        $orderId = $message->invoice_num;
	        if(strtoupper($message->type) == 'AUTH_ONLY')
	        	wm()->get('payment.order')->authorize($orderId,$transactionId);
	        else
	        	wm()->get('payment.order')->charge($orderId,$transactionId);
	    }
	    wm()->get('base.init')->setRenderType('ajax-no-scripts');
	}
	
	public function taskRenderOutput()
	{
		echo CHtml::script('window.location = "'.aUrl('/payment/success').'";');
	}
}